<?php

/*
 * 文件说明.
 * 
 * @author Kun Xu
 */

/**
 * Description of News
 *
 * @author 坤
 */
class News extends ActiveRecord
{
    public $modelName = '新闻';

    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return '{{news}}';
    }

    public function rules()
    {
        return array(
            array('title,author,content,category_id,source', 'required'),
            array('logo,tags', 'safe'),
            array('id,title,logo,tags,author,source,content', 'safe', 'on' => 'search'),
        );
    }

    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'category_id' => '分类',
            'title' => '标题',
            'logo' => '缩略图',
            'author' => '作者',
            'source' => '来源',
            'content' => '正文',
            'tags' => '标签',
            'created' => '创建时间',
            'updated' => '结束时间',
        );
    }

    public function search()
    {
        $criteria = new CDbCriteria( );
        $criteria->compare("id", $this->id);
        $criteria->compare("category_id", $this->category_id);
        $criteria->compare("title", $this->title, TRUE);
        $criteria->compare("logo", $this->logo, TRUE);
        $criteria->compare("tags", $this->tags, TRUE);
        $criteria->compare("author", $this->author, TRUE);
        $criteria->compare("source", $this->source, TRUE);
        $criteria->compare("content", $this->content);
        $criteria->compare("created", $this->created, TRUE);
        $criteria->compare("updated", $this->updated, TRUE);
        return new CActiveDataProvider("News", array(
            "criteria" => $criteria,
            "pagination" => array("pageSize" => 10),
            "sort" => array("defaultOrder" => "t.id DESC")
        ));
    }

    public function relations()
    {
        return array(
            'category' => array(self::BELONGS_TO, 'NewsCategory', 'category_id'),
        );
    }

    public function beforeSave()
    {
//        echo __LINE__;
//        exit;
        if (empty($this->logo)) {
            unset($this->logo);
        }
        $this->updated = time();
        return true;
    }

}
